package com.lixh.roommanager.ui.main

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.github.salomonbrys.kotson.fromJson
import com.google.gson.Gson
import com.lixh.roommanager.MainActivity
import com.lixh.roommanager.R
import com.lixh.roommanager.ui.*
import com.lixh.roommanager.ui.adapter.KAdapterFactory.KAdapter
import com.lixh.roommanager.ui.adapter.KotlinAdapter
import com.lixh.roommanager.ui.bean.Room
import com.lixh.roommanager.ui.utils.GridItemDecoration
import com.lixh.roommanager.ui.utils.UStore
import com.lixh.roommanager.ui.utils.toast
import kotlinx.android.synthetic.main.fragment_main.view.*
import kotlinx.android.synthetic.main.item_room.view.*


/**
 * A placeholder fragment containing a simple view.
 */
class PlaceholderFragment : Fragment() {
    private var roomData by UStore(Constant.JSON_ROOM, "[]")
    var type: Int = 0;
    lateinit var currentView: View

    @SuppressLint("SetTextI18n")
    private var simpleAdapter: KotlinAdapter<Room> = KAdapter {
        layout {
            R.layout.item_room
        }
        bindData { _, vh, room ->
            vh.itemView.tv_room_name.text = room.roomName
            vh.itemView.tv_room_state.text = when (room.state) {
                2 -> "有人"
                4 -> "今日离房"
                3 -> "最后一天了"
                else -> "空闲"
            }
            //全部 和 有人 则设置时间 今日离房
            if (type == 0 || type == 2 || type == 3) {
                val startTime = room.startTime
                val endTime = room.endTime
                if (startTime > 0 && endTime > 0) {
                    val format: String = when (Constant.TIME_TYPE) {
                        0 -> endTime.YMD
                        else -> endTime.YMD_HS
                    }
                    vh.itemView.tv_room_data.text = "到期:$format"
                }
            }

            vh.itemView.setOnLongClickListener {
                //只有全部里才可以删除
                if (type == 0) {
                    val builder =
                        activity?.let { it1 ->
                            AlertDialog.Builder(it1).setTitle("提示：")
                                .setMessage("是否删除，")
                                .setIcon(R.mipmap.ic_launcher)
                                .setCancelable(true) //点击对话框以外的区域是否让对话框消失
                                //设置正面按钮
                                .setPositiveButton(
                                    "确定"
                                ) { dialog, _ ->
                                    remove(vh.adapterPosition)
                                    roomData = Gson().toJson(listData())
                                    toast("删除成功")
                                    dialog.dismiss()
                                }.setNegativeButton("取消") { dialog, _ ->
                                    dialog.dismiss()
                                }
                        }
                    builder?.create()?.show()
                }
                true
            }
            //点击去修改
            vh.itemView.setOnClickListener {
                val intent = Intent(activity, EditActivity::class.java)
                intent.putExtra("index", vh.adapterPosition)
                intent.putExtra("Room", room)
                startActivity(intent)
            }
            //状态 有人 则 设置时间
            when (room.state) {
                2 -> {
                    vh.itemView.card_view.setCardBackgroundColor(Color.parseColor("#ff314b"))
                    vh.itemView.tv_room_name.setTextColor(Color.parseColor("#ffffff"))
                    vh.itemView.tv_room_state.setTextColor(Color.parseColor("#ffffff"))
                    vh.itemView.tv_room_data.setTextColor(Color.parseColor("#ffffff"))
                }
                3 -> {
                    vh.itemView.card_view.setCardBackgroundColor(Color.parseColor("#ffe781"))
                    vh.itemView.tv_room_name.setTextColor(Color.parseColor("#ffffff"))
                    vh.itemView.tv_room_state.setTextColor(Color.parseColor("#ffffff"))
                    vh.itemView.tv_room_data.setTextColor(Color.parseColor("#ffffff"))
                }
                4 -> {
                    vh.itemView.card_view.setCardBackgroundColor(Color.parseColor("#7e3d97"))
                    vh.itemView.tv_room_name.setTextColor(Color.parseColor("#ffffff"))
                    vh.itemView.tv_room_state.setTextColor(Color.parseColor("#ffffff"))
                    vh.itemView.tv_room_data.setTextColor(Color.parseColor("#ffffff"))
                }
                else -> {
                    //闲置
                    vh.itemView.card_view.setCardBackgroundColor(Color.parseColor("#ffffff"))
                    vh.itemView.tv_room_name.setTextColor(Color.parseColor("#000000"))
                    vh.itemView.tv_room_state.setTextColor(Color.parseColor("#000000"))
                    vh.itemView.tv_room_data.setTextColor(Color.parseColor("#000000"))
                }
            }

        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            type = it.getInt(ARG_SECTION_NUMBER)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_main, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        this.currentView = view
        val rvRoom: RecyclerView = currentView.rv_room
        rvRoom.addItemDecoration(GridItemDecoration(2, 7f.dp.toInt(), true))
        this.simpleAdapter into rvRoom
        //房间有人不需要点击事件
        if (type == 2) {
            currentView.tv_empty.text = "房间都是空的哟,加油"
        } else if (type == 3) {
            currentView.tv_empty.text = "今日没有退房的客人"

        }
        currentView.tv_empty.setOnClickListener {
            when (type) {
                2 -> {
                    (activity as MainActivity).switchTab(0)
                }
                else -> {
                    (activity as MainActivity).switchTab(3)
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        "Fragment Resume".LOG
        var rooms =
            Gson().fromJson<List<Room>>(roomData)
        //全部里面不需要 过滤
        if (type != 0) {
            //过滤 2 有人
            rooms = when (type) {
                2 -> {
                    //包含状态 2 和 3 今日到期
                    rooms.filter { it.state == 2 || it.state == 3 }
                }
                3 -> {//今日离店 状态 4
                    rooms.filter { it.state == 4 }
                }
                else -> {
                    //空闲 包含 离店 未打扫
                    rooms.filter { it.state == 1 || it.state == 4 }
                }
            }
        }
        if (rooms.isEmpty()) {
            currentView.tv_empty.visibility = View.VISIBLE
        } else {
            currentView.tv_empty.visibility = View.GONE
        }
        simpleAdapter.replaceData(rooms)

    }


    companion object {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private const val ARG_SECTION_NUMBER = "section_number"

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        @JvmStatic
        fun newInstance(sectionNumber: Int): PlaceholderFragment {
            return PlaceholderFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_SECTION_NUMBER, sectionNumber)
                }
            }
        }
    }
}